package com.restaurant.serviceimpl;
import java.util.List;

import javax.annotation.PostConstruct;

import org.hibernate.SessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.restaurant.domain.Bill;
import com.restaurant.service.BillService;


public class BillServiceImpl implements BillService, BeanNameAware, BeanFactoryAware{

   private HibernateTemplate hibernateTemplate;

   @Autowired
   public void setSessionFactory(SessionFactory sessionFactory){
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Bill addOrModify( Bill objBill ) {
	   
       return ( Bill ) hibernateTemplate.merge( objBill );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Bill deleteRecord( Bill objBill ) {
       hibernateTemplate.delete( objBill );
       return objBill;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Bill modifyRecord( Bill objBill ) {
       return ( Bill ) hibernateTemplate.merge( objBill );
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Bill> loadAll() {
       return (List<Bill>)hibernateTemplate.findByNamedQuery( "Bill.findAll");
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Bill loadByPrimaryKey( Long primaryKey ) {
       return (Bill) hibernateTemplate.get( Bill.class, primaryKey );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Bill deleteByPrimaryKey( Long primaryKey ) {
       return deleteRecord(loadByPrimaryKey(primaryKey));
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Bill> loadByHql(String query) {
       return ( List<Bill> ) hibernateTemplate.find( query );
   }

	@Override
	public void setBeanName(String arg0) {
		System.out.println("setBeanName");
	}

	@Override
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("setBeanFactory");
	}
	
	@PostConstruct
	public void init(){
		System.out.println("init method");
	}
	
}